﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using Century.Model;
using Century.Model.ViewModel;
using Century.EnterpriseLibrary.Caching;


namespace Century.EnterpriseLibrary.Globalization
{
    public static class CharacteristicService
    {

        private static ILog Log
        {
            get
            {
                return Century.EnterpriseLibrary.LogHandler.Loging.LogWithName("CharacteristicService");
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="charType"></param>
        /// <returns></returns>
        public static List<CharValueViewModel> GetCharValues(CharacteristicType charType, int siteId)
        {
            return GetCharValues(charType, siteId);
        }

        internal static List<CharValueViewModel> GetCharValuesFromDB(CharacteristicType charType, string site)
        {
            using (CenturyDbContext db = new CenturyDbContext())
            {
                var charValues = (from c in db.CharValues.Include("CharValueTranslations").Include("CharType")
                                  where c.CharType.TypeName.ToLower() == charType.ToString().ToLower()
                                  && c.CharValueTranslations.Any(q => q.IsAvailable && q.Site == site)
                                  select new CharValueViewModel
                                     {
                                         Id = c.Id,
                                         CharTypeId = c.CharTypeId,
                                         Code = c.Code,
                                         CharValueDescription = c.Description,
                                         CharTypeDescription = c.CharType.TypeName,
                                         Number = c.Number,
                                         //ParentId = q.ParentId,
                                         CurrentSite = site,
                                         CharValueTranslations = c.CharValueTranslations
                                     }).ToList().OrderBy(c => c.OrderInList).ToList();;
                if (!charValues.Any())
                {
                    //Todo: Set Default Site in config
                    charValues = (from c in db.CharValues.Include("CharValueTranslations").Include("CharType")
                                  where c.CharType.TypeName.ToLower() == charType.ToString().ToLower()
                                  select new CharValueViewModel
                                  {
                                      Id = c.Id,
                                      CharTypeId = c.CharTypeId,
                                      Code = c.Code,
                                      CharValueDescription = c.Description,
                                      CharTypeDescription = c.CharType.TypeName,
                                      Number = c.Number,
                                      //ParentId = q.ParentId,
                                      CurrentSite = site,
                                      CharValueTranslations = c.CharValueTranslations
                                  }).ToList().OrderBy(c => c.OrderInList).ToList();;
                    if (!charValues.Any())
                    {
                        return null;
                    }
                    else
                    {
                        foreach (var item in charValues)
                        {
                            CharValueTranslation trans = item.CharValueTranslations.FirstOrDefault(c => c.IsAvailable && c.Site == "en-US");
                            CharValueTranslation newTrans = new CharValueTranslation
                            {
                                CharValueId = item.Id,
                                Site = site,
                                Description = trans == null ? item.CharTypeDescription : trans.Description,
                                IsAvailable = true,
                                OrderInList = trans == null ? 0 : trans.OrderInList
                            };
                            db.CharValueTranslations.Add(newTrans);
                        }
                        db.SaveChanges();
                        charValues = (from c in db.CharValues.Include("CharValueTranslations").Include("CharType")
                                      where c.CharType.TypeName.ToLower() == charType.ToString().ToLower()
                                      && c.CharValueTranslations.Any(q => q.IsAvailable && q.Site == site)
                                      select new CharValueViewModel
                                      {
                                          Id = c.Id,
                                          CharTypeId = c.CharTypeId,
                                          Code = c.Code,
                                          CharValueDescription = c.Description,
                                          CharTypeDescription = c.CharType.TypeName,
                                          Number = c.Number,
                                          //ParentId = q.ParentId,
                                          CurrentSite = site,
                                          CharValueTranslations = c.CharValueTranslations
                                      }).ToList().OrderBy(c => c.OrderInList).ToList();;
                    }
                }
                return charValues;

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="charType"></param>
        /// <param name="site"></param>
        /// <returns></returns>
        public static List<CharValueViewModel> GetCharValues(CharacteristicType charType, string site)
        {
            List<CharValueViewModel> charValueList = CacheService.Get<List<CharValueViewModel>>(string.Format("chtp_{0}_{1}", charType.ToString().ToLower(), site.ToLower()), () => GetCharValuesFromDB(charType, site), new TimeSpan(0, 30, 0));
            return charValueList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="titleCode"></param>
        /// <param name="site"></param>
        /// <returns></returns>
        //public static string GetTitleByTitleCode(int titleCode, string site)
        //{
        //    CharValue charValue = PersistData.AllCharValues.FirstOrDefault(a => a.CharType.TypeName == CharacteristicType.Title.ToString());
        //    if (charValue == null)
        //    {
        //        Log.Error("CharType Table Does not contains Title Key");
        //        return null;
        //    }
        //    //CharValue charValue = (from q in charType.CharValues
        //    //                       where q.Number == titleCode
        //    //                       select q).FirstOrDefault();

        //    //if (charValue == null)
        //    //{
        //    //    Log.Error("CharValue Table Does not contains Value for default country site");
        //    //    return null;
        //    //}
        //    if (charValue.CharValueTranslations.Any(c => c.IsAvailable && c.Site == site))
        //    {
        //        return charValue.CharValueTranslations.First(c => c.IsAvailable && c.Site == site).Description;
        //    }
        //    else
        //    {
        //        return charValue.Description;
        //    }

        //}



        /// <summary>
        /// Method to get Translation of a characteristic value.
        /// </summary>
        /// <param name="charValueId"></param>
        /// <returns></returns>
        //public static string TranslateCharValue(int charValueId)
        //{
        //    return string.Empty;
        //}




    }
}
